package com.rxt.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rxt.common.result.Result;
import com.rxt.mapper.PProductMapper;
import com.rxt.model.domain.PProduct;
import com.rxt.model.domain.PProductSku;
import com.rxt.model.vo.ProductVo;
import com.rxt.service.IPProductService;
import com.rxt.util.BaseContext;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 周梓文
 * @since 2023-08-24
 */
@Slf4j
@Api(tags = "商品模块")
@RestController
@RequestMapping("/pProduct")
public class PProductController {

    @Autowired
    private IPProductService ipProductService;

    @ApiOperation(value = "获取热门商品")
    @GetMapping("/getHot")
    public Result getHotProd() {
        List<PProduct> pProduct = ipProductService.getHotProd();
        log.info("hot:{}",pProduct);
        return Result.ok(pProduct);
    }

    @ApiOperation(value = "查看详情")
    @GetMapping("/getDetail/{id}")
    public Result getDetail(@PathVariable Integer id) {
        ProductVo productVo = ipProductService.getDetails(id);
        return Result.ok(productVo);
    }

    @ApiOperation("商品分页")
    @GetMapping("/{page}/{limit}")
    public Result page(@PathVariable Integer page, @PathVariable Integer limit, String keyWord) {
        Page<PProduct> productPage = new Page<>(page,limit);
        LambdaQueryWrapper<PProduct> wrapper = new LambdaQueryWrapper<>();
        // wrapper.ne(PProduct::getUserId, BaseContext.get().getId());
        if (!StringUtils.isEmpty(keyWord)){
            wrapper.like(PProduct::getTitle,keyWord);
        }
        IPage<PProduct> page1 = ipProductService.page(productPage, wrapper);
        return Result.ok(page1);
    }

    @ApiOperation("批量删除商品")
    @DeleteMapping
    public Result delete(List<Integer> ids) {
        ipProductService.deleteBatch(ids);
        return Result.ok();
    }

    @ApiOperation("根据id删除商品")
    @DeleteMapping("byId")
    public Result delById(Long id) {
        return Result.ok(ipProductService.removeById(id));
    }

    @ApiOperation("获取价格")
    @GetMapping("/getPrice")
    public Result getPrice(Integer id,String sku) {
        PProductSku productSku = ipProductService.getPrice(id,sku);
        return Result.ok(productSku.getPrice());
    }

    @ApiOperation("添加商品")
    @PostMapping("/addProd")
    public Result addProd(@RequestBody ProductVo productVo) {
        ipProductService.addProd(productVo);
        return Result.ok();
    }

    @ApiOperation("我的商品")
    @GetMapping("/myProd")
    public Result getMyProd() {
        List<PProduct> products = ipProductService.getMyProd();
        return Result.ok(products);
    }
}

